export default {
	methods: {
		//开始考试
		startAnswer(apiSetting, answerPageSettings, navigateType = 'navigateTo') {
			let {
				method,
				url,
				params
			} = apiSetting
			method = method || 'post';
			url = url || '/app/exam/startAnswerPaper'
			uni.showLoading({
				mask: true,
				title: '加载试卷中...'
			});
			this.request[method](url, params)
				.then(res => {
					if (res.exercisesQuestion.length === 0) {
						uni.showToast({
							title: "暂无试题！",
							icon: "none"
						})
						return;
					}
					uni.setStorageSync('topicList', res);
					uni[navigateType]({
						url: `/pages/answer/answer?setting=${JSON.stringify(answerPageSettings)}`,
						fail() {
							console.log(arguments)
						}
					});
				})
				.finally(() => {
					uni.hideLoading();
				});
		},
		startExam(exam, ...args) {
			//考试 答题
			const {
				examid,
				ueid,
				duration,
				paperid,
				upid,
				number,
				minutes: totalSeconds,
				endtime
			} = exam;
			const answerPageSetting = {
				leftBtnMode: 'jiaojuan',
				countDown: true,
				timestamp: totalSeconds - (duration || 0),
				totalSeconds,
				topicNumber: number,
				backHandler: 'confirmBack',
				updateHistoryHandler: 'updateExamHistory',
				updateTiming: 'change',
				endtime
			};
			this.startAnswer({
				params: {
					examid,
					ueid,
					paperid,
					upid
				}
			}, answerPageSetting, ...args);
		},
		startUserPaper(paper, ...args) {
			// console.log(paper);
			//在线组卷 答题

	
			const {
				paperid,
				duration,
				upid,
				number
			} = paper;
			const params = {
				paperid,
				upid
			};
			 // 再考一次时间计算
			const totalSeconds=paper.minutes?paper.minutes:paper.duration;
			const starAgin=paper.minutes?totalSeconds - (duration || 0):paper.duration;
			
			const answerPageSettings = {
				leftBtnMode: 'jiaojuan',
				backHandler: 'confirmBack',
				updateHistoryHandler: 'updateExamHistory',
				updateTiming: 'change',
				timestamp: starAgin,
				totalSeconds,
				countDown: true,
				isCountDown: true,
				topicNumber: number,
				achievementPageReexamHander: 'startUserPaper'
			};
			
			this.startAnswer({
				params
			}, answerPageSettings, ...args);
		},
		startMoni(paper, ...args) {
			//全真模拟 模拟考试
			const {
				paperid,
				upid,
				number: topicNumber = null,
				duration,
				isSign,
				isClock
			} = paper;
			const params = {
				paperid,
				isClock,
				upid
			};
			const answerPageSettings = {
				leftBtnMode: 'jiaojuan',
				backHandler: 'confirmBack',
				updateHistoryHandler: 'updateExamHistory',
				updateTiming: 'change',
				topicNumber,
				timestamp:duration,
				countDown: true,
				isCountDown: false,
				isSign,
				isClock,
				achievementPageReexamHander: 'startMoni'
			};
			this.startAnswer({
				params
			}, answerPageSettings, ...args);
		},
		startYaTi(paper, ...args) {
			const {
				paperid,
				upid,
				number: topicNumber,
				duration: timestamp
			} = paper;
			const params = {
				paperid,
				upid
			};
			const answerPageSettings = {
				leftBtnMode: 'jiaojuan',
				backHandler: 'confirmBack',
				updateHistoryHandler: 'updateExamHistory',
				updateTiming: 'change',
				topicNumber,
				timestamp,
				countDown: true,
				isCountDown: false,
				achievementPageReexamHander: 'startYaTi'
			};
			this.startAnswer({
				params
			}, answerPageSettings, ...args);
		},
		//清除答题记录
		clearHistory(chapterId, type) {
			return this.request.delete(
				`/app/exercise/deleteSchedule/${chapterId}/${type}`)
		},
		//查看成绩页
		viewResult({
			upid,
			status
		}, reexamHandler, reload, navigateType = 'navigateTo') {
			if (status !== 2) return;
			uni[navigateType]({
				url: `/pages/answer/achievement/achievement?upid=${upid}&reload=${reload}&reexamHandler=${reexamHandler}`
			})
		},
		// 打卡结果页
		signResult({
			upid,
			status
		}, reexamHandler, reload, navigateType = 'navigateTo') {
			if (status !== 2) return;
			uni[navigateType]({
				url: `/pages/dailyPractice/design?upid=${upid}&reload=${reload}&reexamHandler=${reexamHandler}`
			})
		}
	}
}
